package cfs.model;

import org.mybeans.dao.DAOException;
import org.mybeans.dao.GenericDAO;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import cfs.databean.Customer;

public class CustomerDAO extends GenericDAO<Customer> {

	public CustomerDAO() {
		super(Customer.class, "customer", "customer_id");
		setUseAutoIncrementOnCreate(true);
		// Long running web apps need to clean up idle database connections.
		// So we can tell each BeanTable to clean them up. (You would only
		// notice
		// a problem after leaving your web app running for several hours.)
		getTable().setIdleConnectionCleanup(true);
	}

	public Customer lookup(String username) throws DAOException {
		try {
			Customer[] users = getFactory().match(
					MatchArg.equals("username", username));
			if (users.length <= 0) {
				return null;
			} else {
				return users[0];
			}
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public void changePassword(int customer_id, String password)
			throws DAOException {
		try {
			Transaction.begin();
			Customer db_cus = getFactory().lookup(customer_id);
			if (db_cus == null) {
				throw new DAOException("Customer id " + customer_id
						+ "does not exists");
			}
			db_cus.setPassword(password);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive())
				Transaction.rollback();
		}
	}

	public void setPassword(int customer_id, String password)
			throws DAOException {
		try {
			Transaction.begin();
			Customer db_cus = getFactory().lookup(customer_id);
			if (db_cus == null) {
				throw new DAOException("Customer id " + customer_id
						+ "does not exists");
			}
			db_cus.setPassword(password);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive())
				Transaction.rollback();
		}
	}

	public Customer getCustomerByUserName(String username) throws DAOException {
		try {
			Customer[] cus = this.getFactory().match(
					MatchArg.equals("username", username));
			if (cus.length == 0)
				return null;
			else
				return cus[0];
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public Customer[] getUsers() throws DAOException {
		try {
			Customer[] users = this.getFactory().match();
			return users;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public long getAvailableBalance(int customer_id) throws DAOException {
		Customer user;
		try {
			user = this.getFactory().lookup(customer_id);
			return user.getAvailablebalance();
		} catch (RollbackException e) {
			throw new DAOException(e);
		}

	}

	public void updateAvailableBalance(int customer_id, long newAvailableBalance)
			throws DAOException {
		try {
			Transaction.begin();
			Customer db_cus = getFactory().lookup(customer_id);
			if (db_cus == null) {
				throw new DAOException("Customer" + customer_id
						+ "no longer exists");
			}
			db_cus.setAvailablebalance(newAvailableBalance);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive())
				Transaction.rollback();
		}
	}

}
